package com.tron.game.controllers;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tron.game.Lira;
import com.tron.game.LiraHelper;
import com.tron.game.configs.BscConfig;
import com.tron.game.configs.TronConfig;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.tron.trident.abi.datatypes.Bool;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tuples.generated.Tuple14;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.DefaultGasProvider;
import org.web3j.utils.Convert;


import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/bsc")
public class BscController {
    @Autowired
    BscConfig tronConfig;

    //.\web3j truffle generate F:\OtherProducts\tron\bsc_token\build\contracts\lira.json -o F:\OtherProducts\tron\bsc_token\build\java -p token
    @RequestMapping("/")
    String index() throws Exception {
        Lira lira = LiraHelper.getInstance();
        BigInteger time = lira.askprice().send();
        return time.toString();
    }

    @RequestMapping("/getCurrentRoundInfo")
    JSONObject getCurrentRoundInfo() throws Exception {
        //Nile test net, using a node from Nile official website
        Lira lira = LiraHelper.getInstance();
        JSONObject object = new JSONObject();
        if (lira == null) {
            //string
            object.put("status", "fail");
            return object;
        }
//            1  uint256 game_id,
//            2  string memory name,
//            3    uint256 begin_time,
//            4    uint256 game_times,
//            5   uint256 last_buy_time,
//            6    uint256 over_time,
//            7    uint256 price,
//            8    uint256 price_upper_rate,
//            9    Entitys.GameStatus status,
//            10    uint256 prize_value,
//            11    uint256 bonus_value,
//            12    uint256 buy_count,
//            13   address last_buy_user_address,
//            14    uint256 next_change_rate_count
        Tuple14<BigInteger, String, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, BigInteger, String, BigInteger>
                res = lira.getCurrentRoundInfo().send();


        BigInteger game_id = res.component1();
        String name = res.component2();
        BigInteger begin_time = res.component3();
        BigInteger game_times = res.component4();
        BigInteger last_buy_time = res.component5();
        BigInteger over_time = res.component6();
        BigInteger price = res.component7();
        BigInteger price_upper_rate = res.component8();
        BigInteger status = res.component9();
        BigInteger prize_value = res.component10();
        BigInteger bonus_value = res.component11();
        BigInteger buy_count = res.component12();
        String last_buy_user_address = res.component13();
        BigInteger next_change_rate_count = res.component14();


        //string
        object.put("game_id", game_id);
        object.put("name", name);
        object.put("begin_time", begin_time);
        object.put("begin_time_date", new Date(begin_time.longValue() * 1000));

        object.put("game_times", game_times);
        object.put("last_buy_time", last_buy_time);
        object.put("last_buy_time_date", new Date(last_buy_time.longValue() * 1000));

        object.put("over_time", over_time);
        object.put("over_time_date", new Date(over_time.longValue() * 1000));

        object.put("price", price.longValue() / Math.pow(10, 6));
        object.put("price_upper_rate", price_upper_rate.longValue() / Math.pow(10, 18));
        object.put("prize_value", prize_value.longValue() / Math.pow(10, 6));
        object.put("buy_count", buy_count);
        object.put("last_buy_user_address", last_buy_user_address);
        object.put("next_change_rate_count", next_change_rate_count);
        return object;


    }

    @RequestMapping("/checkGameAndPrize")
    String checkGameAndPrize() throws Exception {
        Lira lira = LiraHelper.getInstance();

        if (lira == null) {

            return "fail";
        }
        Boolean result = lira.checkGameAndPrize().send();
        return result ? "true" : "false";
    }

    @RequestMapping("/getNow")
    long getNow() throws Exception {
        Lira lira = LiraHelper.getInstance();

        if (lira == null) {

            return -1;
        }
        BigInteger result=lira.getNow().send();
        return  result.longValue();
    }
    @RequestMapping("/PrizeGame")
    String  PrizeGame() throws Exception {
        Lira lira = LiraHelper.getInstance();

        if (lira == null) {

            return "fail";
        }
        TransactionReceipt transactionReceipt= lira.PrizeGame().send();
        return transactionReceipt.getBlockHash();
    }

    @RequestMapping("/BuyToken")
    String  BuyToken() throws Exception {
        Lira lira = LiraHelper.getInstance();

        if (lira == null) {

            return "fail";
        }
        BigInteger price= lira.askprice().send();

        TransactionReceipt transactionReceipt= lira.buy_token("0xEF000629F1C5A560A515b5c38cd0EF03f8a5d704",price).send();
        return transactionReceipt.getBlockHash();
    }
}